package cn.ittaoli.ajax.controller;

import cn.ittaoli.ajax.model.Slide;
import cn.ittaoli.ajax.service.SlideService;
import cn.ittaoli.ajax.utils.FileUpload;
import cn.ittaoli.ajax.utils.IDUtils;
import cn.ittaoli.ajax.utils.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by lub_lenovo on 2017/8/8.
 */
@Controller
@RequestMapping("/slide")
public class SlideController {
    @Autowired
    private SlideService slideService;

    @RequestMapping("/slideList")
    public String slideList(Model model, @ModelAttribute("msg") String msg, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer numPerPage){

//        PageInfo<Slide> slides=slideService.getByPage(pageNum,numPerPage);




        List<Slide> slides=slideService.getAll();
        model.addAttribute("slide",slides);
        model.addAttribute("menu","轮播图列表");
        model.addAttribute("msg",msg);

        return "slide/slideList";
    }

    @RequestMapping("/slideAdd")
    public String slideAdd(Model model){
        model.addAttribute("menu","新增轮播图");
        return "slide/slideAdd";
    }

    @RequestMapping("/insertSlide")
    public String insertSlide(HttpServletRequest request, MultipartFile file,Slide slide,RedirectAttributes attr) throws IOException {
        String path= FileUpload.upload(file,request);

        slide.setImg(path);

        int res=slideService.inserSlide(slide);
        if(res>0){
            attr.addAttribute("msg","添加成功");
            return "redirect:slideList.do";
        }else{
            return "slide/slideAdd";
        }
    }
}
